#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 1e3 + 10;
int g[N][N];
int st[N];
int n, L, K;
queue<PII>q;
int bfs(int x)
{
	while (q.size())q.pop();
	memset(st, 0, sizeof(st));
	st[x] = 1;
	q.push({ x,0 });
	int ans = 0;
	while (q.size()) {
		PII t = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (!st[i] && g[t.x][i]) {
				st[i] = 1;
				if (t.y + 1 <= L) {
					q.push({ i,t.y + 1 });
					ans++;
				}
			}
		}
	}
	return ans;
}
int main()
{
	cin >> n >> L;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		while (x--) {
			int t;
			cin >> t;
			g[t][i] = 1;
		}
	}
	cin >> K;
	while (K--) {
		int x;
		cin >> x;
		cout << bfs(x) << endl;
	}
	return 0;
}